'use strict';
const fs = require('fs');
const path = require('path');
const log4js = require('log4js');

const cfgUtil = require('./configuration');
const serverCfg = cfgUtil.get('server');

const LOGS_DIR = serverCfg.LOGS_DIR;

/*
* 日志对象获取
*/

class Log {
  constructor() {
    this._defaults = {};
    //创建日志文件夹
    if(!fs.existsSync(LOGS_DIR)) {
      fs.mkdirSync(LOGS_DIR);
    }
    log4js.configure(path.join(serverCfg.CONF_DIR, 'log4js.json'));
  }

  getLogger(name) {
    if(!name) {
      return this;
    }
    let _defaults = this._defaults;
    let logger = _defaults[name];
    if(!logger) {
      logger = log4js.getLogger(name);
    }
    return logger;
  }
}

const instance = new Log();
module.exports = instance;